package com.rc.evcharger.dto.msg;


import com.rc.evcharger.common.annotation.ByteLength;
import com.rc.evcharger.common.base.AbstractTerminalMessage;
import com.rc.evcharger.common.type.DataType;
import com.rc.evcharger.common.utils.DataUtil;

import java.lang.reflect.Field;

/**
 * 晶福源充电桩发送的数据
 *
 * @author yang
 */
public class TerminalMessage extends AbstractTerminalMessage {
    private static final long serialVersionUID = -3813389249597127881L;

    @ByteLength(length = 1)
    private String start_sign;//帧起始符"68H"
    @ByteLength(length = 6)
    private String rtua;//终端逻辑地址
    @ByteLength(length = 2)
    private String msta;//主站地址与命令序号
    @ByteLength(length = 1)
    private String contral;//控制码
    @ByteLength(length = 2)
    private String length;//数据长度
    @ByteLength(length = 0)
    private String data;//数据域
    @ByteLength(length = 1)
    private String cs;//校验码
    @ByteLength(length = 1)
    private String end_sign;//结束码

    private String body;//消息主体

    private long currentTimeMillis;     //时间戳

    public TerminalMessage() {
        super();
    }

    public TerminalMessage(String rtua, String contral, DataType dataType) {
        this.rtua = rtua;
        this.contral = contral;
        this.data = dataType.createFrame();
        this.length = dataType.getLength();//getLength方法要在createFrame方法后调用
    }

    /**
     * 发送停止命令，余额不足停止方式;
     *
     * @param rtua
     * @param contral
     * @param dataType
     * @param stopReason
     */
    public TerminalMessage(String rtua, String contral, DataType dataType, String stopReason) {
        this.rtua = rtua;
        this.contral = contral;
        this.data = dataType.createFrame();
        this.length = dataType.getLength();//getLength方法要在createFrame方法后调用
        //修改订单号第二位字符为1是余额不足停止， 2.电价异常,3.电流过低,4.充满自动停止;
        this.data = this.data.trim();
        StringBuilder sb = new StringBuilder(this.data);
        sb.replace(10, 11, stopReason);
        this.data = sb.toString();
    }

    public TerminalMessage(int start, int end, String[] bcdCode) throws Exception {
        int index = start;
        Field[] fields = this.getClass().getDeclaredFields();
        StringBuffer sb = null;
        for (Field field : fields) {
            ByteLength bl = field.getAnnotation(ByteLength.class);
            if (bl == null) {
                continue;
            }
            int length = bl.length();
            if (field.getName().equals("data")) {
                String str = this.length.substring(0, 2);
                length = Integer.parseInt(str, 16);
            }
            if (field.getName().equals("contral")) {//跳过  主站后面的的68帧头
                if (!this.start_sign.equals(bcdCode[index]))
                    throw new Exception();
                index++;
            }
            sb = new StringBuffer();
            for (int i = index; i < index + length; i++) {
                sb.append(bcdCode[i]).append(DataUtil.SPLIT);
            }
            index += length;
            try {
                field.set(this, sb.toString().trim());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (!this.end_sign.equals(bcdCode[end])) {
            throw new Exception();
        }
    }

    public String getStart_sign() {
        return start_sign;
    }

    public void setStart_sign(String start_sign) {
        this.start_sign = start_sign;
    }

    public String getRtua() {
        return rtua;
    }

    public void setRtua(String rtua) {
        this.rtua = rtua;
    }

    public String getMsta() {
        return msta;
    }

    public void setMsta(String msta) {
        this.msta = msta;
    }

    public String getContral() {
        return contral;
    }

    public void setContral(String contral) {
        this.contral = contral;
    }

    public String getLength() {
        return length;
    }

    public void setLength(String length) {
        this.length = length;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getCs() {
        return cs;
    }

    public void setCs(String cs) {
        this.cs = cs;
    }

    public String getEnd_sign() {
        return end_sign;
    }

    public void setEnd_sign(String end_sign) {
        this.end_sign = end_sign;
    }

    public long getCurrentTimeMillis() {
        return currentTimeMillis;
    }

    public void setCurrentTimeMillis(long currentTimeMillis) {
        this.currentTimeMillis = currentTimeMillis;
    }

    @Override
    public String toString() {
        return "JfyMessage [start_sign=" + start_sign + ", rtua=" + rtua + ", msta=" + msta + ", contral=" + contral + ", length="
                + length + ", data=" + data + ", cs=" + cs + ", end_sign=" + end_sign + "]";
    }

    /**
     * @return the body
     */
    public String getBody() {
        return start_sign + " " + rtua + " " + msta + " " + start_sign + " " + contral + " "
                + length + " " + data + " " + cs + " " + end_sign + " " + currentTimeMillis;
    }

    /**
     * @param body the body to set
     */
    public void setBody(String body) {
        this.body = body;
    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("01 02 00 00 01 05");
        sb.replace(10, 11, "1");
        System.out.println(sb.toString());
    }
}
